package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.ApiMapping;
import com.qfedu.openplateform.manager.entity.Menu;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.ApiMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: 叶上秋
 * @date: 2021/10/27 17:16
 * @version: 1.0
 * @description: 路由管理
 */
@RestController
@RequestMapping("/sys/api_mapping")
public class ApiMappingController {

    @Autowired
    private ApiMappingService apiMappingService;

    @RequestMapping("/table")
    public TableData findByPageApiMapping(@RequestParam(value = "page",defaultValue = "1",required = false) Integer pageNum,
                                          @RequestParam(value = "limit",defaultValue = "10",required = false) Integer pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        List<ApiMapping> allApiMapping = apiMappingService.findAllApiMapping();
        PageInfo<ApiMapping> pageInfo = new PageInfo<>(allApiMapping);

        return new TableData(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 添加路由
     * @param apiMapping 传的参数
     */
    @RequestMapping("/add")
    public void addApiMapping(ApiMapping apiMapping){
        apiMappingService.addApiMapping(apiMapping);
    }

    /**
     * 根据id删除路由
     * @param ids 需要删除的路由ids
     * @return
     */
    @RequestMapping("/del")
    public AjaxMessage deleteApiMapping(Integer[] ids){
        try {
            apiMappingService.deleteApiMapping(ids);
            return new AjaxMessage(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxMessage(true, "删除失败");
        }
    }

    @RequestMapping("/update")
    public AjaxMessage update(ApiMapping apiMapping) {
        try {
            apiMappingService.updateApiMapping(apiMapping);
            return new AjaxMessage(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxMessage(true, "修改失败");
        }
    }
}
